package supimpa.src;

import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

public class Camera {

	public float pos[] = new float[3];
	public float yaw, pitch, roll;
	
	private float speed = 0.025f;
	
	private boolean mouseDown, W, A, S, D, SPACE;
	public void updateCamera(){
		
		float deltaX = (float)Mouse.getDX() /10f;
		float deltaY = (float)Mouse.getDY() /10f;		
		
		mouseDown = Mouse.isButtonDown(0);		
		
		if (Mouse.isGrabbed()){
			
			onRotateX(deltaX);
			onRotateY(deltaY);
		}
		
		W = Keyboard.isKeyDown(Keyboard.KEY_W);
		A = Keyboard.isKeyDown(Keyboard.KEY_A);
		S = Keyboard.isKeyDown(Keyboard.KEY_S);
		D = Keyboard.isKeyDown(Keyboard.KEY_D);
		SPACE = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
		
		if(W){
			
			pos[0] += speed * (float)Math.sin(Math.toRadians(yaw));
            pos[2] -= speed * (float)Math.cos(Math.toRadians(yaw));
				//pos[1] += speed * (float)Math.tan(Math.toRadians(yaw));
			
		}else if(S){
			
			pos[0] -= speed * (float)Math.sin(Math.toRadians(yaw));
            pos[2] += speed * (float)Math.cos(Math.toRadians(yaw));
            	//pos[1] -= speed * (float)Math.tan(Math.toRadians(yaw));
		}
		if(A){
			
			pos[0] += speed * (float)Math.sin(Math.toRadians(yaw-90));
            pos[2] -= speed * (float)Math.cos(Math.toRadians(yaw-90));
		}
		if(D){
			
			pos[0] += speed * (float)Math.sin(Math.toRadians(yaw+90));
            pos[2] -= speed * (float)Math.cos(Math.toRadians(yaw+90));
		}
		if(SPACE){pos[1] += speed;}
	}
	
	public void transformWorld(){
		
	    glRotatef(pitch, 1, 0, 0);
	    glRotatef(yaw, 0, 1, 0);
	    glRotatef(roll, 0, 0, 1);
	    glTranslatef(-pos[0], -pos[1], -pos[2]);
	}
	
	
	
	
	
	
	
	
	public void onRotateX(float delta){
		
		yaw += delta;
		while (yaw > 360f){yaw -= 360f;}
	}
	
	public void onRotateY(float delta){
		
		boolean limitVision = true;
		
		if ( !limitVision || (pitch >= -90f && pitch <= 90f) ){
			
			pitch -= delta;
			
			if (limitVision){pitch = pitch > 90f ? 90f : pitch < -90f ? -90f : pitch;}
		}
	}
}
